<template>
  <div class="datasource-content-list" id="table-drag">
    <div
      v-for="item in data"
      :key="item.id"
      class="datasource-content-item"
      :class="{ 'has-used': allNodeTitle.includes(item.title) }"
      draggable="true"
      @dragstart="onDrag($event, item)"
      @dragend="onDragEnd"
    >
      <icon-file />
      <span class="title" :title="item.table_comment + item.table_name">
        {{ item.table_comment + " " + item.table_name }}
      </span>
      <a-space>
        <icon-exclamation-circle @click="handleClick(item)" />
        <icon-edit v-if="item.is_view" @click="editView(item)" />
      </a-space>
    </div>

    <!--字段详情-->
    <a-modal v-model:visible="visible">
      <div class="table-info">
        <div class="table-info-header">{{ current.table_name }}</div>
        <div class="table-info-title">{{ current.table_comment }}</div>
        <div class="fields-number">包含字段{{ fields.length }}</div>
        <a-divider style="margin: 4px 0 16px" />
        <div class="table-info-fields">
          <div class="table-info-field-line" v-for="field in fields" :key="field.field">
            <div class="table-field-icon">
              <field-type-icon :type="field.dataType" />
            </div>
            <div class="table-field-name">
              <span>{{ field.name }}</span>
            </div>
            <div class="table-field-title">
              <span>{{ field.comment }}</span>
            </div>
          </div>
        </div>
      </div>
    </a-modal>
    <cube-table-view v-if="isEditView" v-model:visible="isEditView" v-model="current" />
  </div>
</template>

<script>
import { Message as message } from "@arco-design/web-vue"
import { reactive, toRefs } from "vue"
import tool from "@/utils/tool.js"

const { deepCopy } = tool
import FieldTypeIcon from "@/components/Service/LinkNode/field-type-icon.vue"
import source from "@/views/datasource/api/source.js"
import CubeTableView from "@/views/datasource/cube/components/cube-table-view.vue"

const { getFieldsList: getTablesFields } = source

export default {
  name: "node-list",
  components: { CubeTableView, FieldTypeIcon },
  props: {
    data: {
      type: Array,
      default: () => []
    },
    allNodeTitle: {
      type: Array,
      default: () => []
    },
    rootNode: {
      type: Object,
      default: () => ({})
    },
    loading: {
      type: Boolean
    }
  },
  emits: ["start", "end"],
  setup: function (props, { emit }) {
    const states = reactive({
      visible: false,
      isEditView: false,
      fields: [],
      current: {}
    })

    // 表格拖拽开始
    function onDrag(e, item) {
      if (props.rootNode.id === item.id) {
        message.warning("当前数据表已经存在，请不要重复关联！")
        return
      }
      e.dataTransfer.setData("id", item.id)
      emit("start")
    }

    function onDragEnd() {
      emit("end")
    }

    // 表格点击事件
    function handleClick(table) {
      getTablesFields({
        datasource_id: table.datasource_id,
        table_name: table.table_name
      }).then((res) => {
        states.fields = res.data
        states.visible = true
        states.current = deepCopy(table)
      })
    }
    const editView = (item) => {
      states.isEditView = !states.isEditView
      item.is_edit = 1
      item.table_name = item.table_comment || item.tableName
      item.view_name = item.tableName
      states.current = item
    }
    return {
      ...toRefs(states),
      onDrag,
      onDragEnd,
      handleClick,
      editView
    }
  }
}
</script>

<style scoped lang="less">
.datasource-content-list {
  position: relative;
}

.datasource-content-item {
  position: relative;
  display: flex;
  align-items: center;
  font-size: 12px;
  cursor: pointer;
  overflow: hidden;
  padding: 8px;

  .b-icon-file-text {
    display: inline-block;
    height: 18px;
    margin-right: 4px;
  }

  .title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 1.5;
    flex: 1;
  }

  .b-icon-info-circle {
    position: absolute;
    right: 5px;
    top: 6px;
    display: none;

    &:hover {
      color: getColor();
    }
  }

  &.has-used {
    .b-icon-file-text {
      color: getColor();
    }
  }

  &:hover {
    background: #ededed;

    .b-icon-info-circle {
      display: block;
    }
  }
}

.table-info {
  .table-info-header {
    margin-bottom: 3px;
  }

  .table-info-title {
    opacity: 0.5;
  }

  .fields-number {
    opacity: 0.6;
  }

  .table-info-fields {
    max-height: 400px;
    min-height: 160px;
    overflow: auto;

    .table-info-field-line {
      display: flex;
      align-items: center;
      height: 30px;
    }

    .table-field-icon {
      margin-right: 8px;
      display: flex;
      padding-top: 1px;
      color: getColor();
    }

    .table-field-name,
    .table-field-title {
      flex-basis: 200px;
      color: rgba(0, 0, 0, 0.85);
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .table-field-title {
      color: #969696;
    }
  }
}
</style>
